package logic

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"srv/model/mysql"
	"srv/pkg"
	"strconv"

	"srv/internal/svc"
	"srv/rent"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddOrderLogic {
	return &AddOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddOrderLogic) AddOrder(in *rent.AddOrderRequest) (*rent.AddOrderResponse, error) {
	g := mysql.Goods{}

	orderSn := uuid.NewString()
	amount := g.GoodsPrice * in.Amount
	o := mysql.Order{
		OrderSn:    orderSn,
		UserId:     int(in.UserId),
		Amount:     float64(amount),
		Status:     int(in.Status),
		Payment:    int(in.Payment),
		GoodsTitle: in.GoodsTitle,
		GoodsPrice: float64(in.GoodsPrice),
		GoodsId:    int(in.GoodsId),
		GoodsCover: in.GoodsCover,
	}
	if o.UserId < 0 {
		logx.Error("用户未登录，不能下单")
		return nil, nil
	}
	/*if int64(g.GoodsNum) < in.Status {
		logx.Error("库存不足")
		return nil, nil
	}*/

	price := strconv.Itoa(int(amount))
	url := pkg.AliPay(orderSn, price)
	fmt.Println(url)

	return &rent.AddOrderResponse{}, nil
}
